// Connection.cpp: implementation of the Connection class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "Connection.h"

#include "CommonLib\Structure.h"
#include "CommonLib\Packet.h"

#include "CommServerLib\CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MyConnection::MyConnection():
	m_hClient(NULL),
	m_strIPAddr(""),
	m_Time(),
	m_Type(ConnectionType::Unknown),
	//m_nConnectCount(0),
	m_nState(0),
	m_nId(-1),
	m_bEflongClient(0),
	m_strUserId("")
{
}

MyConnection::~MyConnection()
{
}

void MyConnection::NotifyCoalMineSendingDefinition() const
{
	Packet packetNotification(sizeof(VizClientServer));
	VizClientServer * pVcs = (VizClientServer *)packetNotification.GetpData();
	pVcs->m_dwMethodType = VCS_NT_SENDDEFINITION;
	CommWithClient::Instance()->SendTo(packetNotification, m_hClient);
}

void MyConnection::NotifyCoalMineSendingData() const
{
	Packet packetNotification(sizeof(VizClientServer));
	VizClientServer * pVcs = (VizClientServer *)packetNotification.GetpData();
	pVcs->m_dwMethodType = VCS_NT_SENDDATA;
	CommWithClient::Instance()->SendTo(packetNotification, m_hClient);
}

void MyConnection::NotifyClientUpdatingDefinition(DefinitionUpdateStruct * pDefinitionUpdateStruct) const
{
	pDefinitionUpdateStruct->m_dwMethodType = VCS_NT_UPDATEDEFINITION;
	Packet packetNotification(pDefinitionUpdateStruct->m_dwBufSize, (LPBYTE)pDefinitionUpdateStruct);
	CommWithClient::Instance()->SendTo(packetNotification, m_hClient);
}

void MyConnection::NotifyClientUpdatingDefinition(DefinitionUpdateStructEx * pDefinitionUpdateStruct) const
{
	pDefinitionUpdateStruct->m_dwMethodType = VCS_NT_UPDATEDEFINITION_EX;
	Packet packetNotification(pDefinitionUpdateStruct->m_dwBufSize, (LPBYTE)pDefinitionUpdateStruct);
	CommWithClient::Instance()->SendTo(packetNotification, m_hClient);
}

void MyConnection::NotifyClientUpdatingData(DataUpdateStruct * pDataUpdateStruct) const
{
	pDataUpdateStruct->m_dwMethodType = VCS_NT_UPDATEDATA;
	Packet packetNotification(pDataUpdateStruct->m_dwBufSize, (LPBYTE)pDataUpdateStruct);
	CommWithClient::Instance()->SendTo(packetNotification, m_hClient);
}

void MyConnection::NotifyClientUpdatingData(DataUpdateStructEx * pDataUpdateStruct) const
{
	pDataUpdateStruct->m_dwMethodType = VCS_NT_UPDATEDATA_EX;
	Packet packetNotification(pDataUpdateStruct->m_dwBufSize, (LPBYTE)pDataUpdateStruct);
	CommWithClient::Instance()->SendTo(packetNotification, m_hClient);
}

void MyConnection::NotifyClientConnectionStatusChanged(ConnectionStruct * pConnectionStruct) const
{
	Packet packetNotification(sizeof(ConnectionStruct), (LPBYTE)pConnectionStruct);
	CommWithClient::Instance()->SendTo(packetNotification, m_hClient);
}

int MyConnection::InsertToListCtrl(CListCtrl& listctrl) const
{
	LVITEM curListItem;

	curListItem.mask = LVIF_TEXT | LVIF_PARAM;
	curListItem.iItem = 0;
	curListItem.iSubItem=0;
	CString str;
	str.Format("%08X", m_hClient);
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	curListItem.lParam = (LPARAM)this;
	int iActualItem = listctrl.InsertItem(&curListItem);
	str.UnlockBuffer();

	curListItem.mask = LVIF_TEXT;
	curListItem.iItem = iActualItem;
	curListItem.iSubItem = 1;
	str = m_strIPAddr;
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	listctrl.SetItem(&curListItem);
	str.UnlockBuffer();

	curListItem.iSubItem = 2;
	str = m_Time.Format("%Y.%m.%d %H:%M:%S");
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	listctrl.SetItem(&curListItem);
	str.UnlockBuffer();

	curListItem.iSubItem = 3;
	str = ConnectionType::GetName(m_Type);
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	listctrl.SetItem(&curListItem);
	str.UnlockBuffer();

	curListItem.iSubItem = 4;
	str = m_strUserId;
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	listctrl.SetItem(&curListItem);
	str.UnlockBuffer();

	listctrl.SetItemState(iActualItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

	return iActualItem;
}

void MyConnection::SetCurrTime(long nId)
{
	if(m_nId == nId || m_nIdList[nId] == nId){
		m_DataUpdateTime = CTime::GetCurrentTime();
	}
}

void MyConnection::AddCoalMineId(long nId)
{
	m_nIdList[nId] = nId;
}